package com.szjlc.blog.common.exception;

import com.szjlc.blog.common.enums.ResultCodeEnum;
import com.szjlc.blog.common.result.WebServiceResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常
 * @author steve
 * @since 2022-11-13 15:20
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = ShiroException.class)
    public WebServiceResponse handler(ShiroException e) {
        log.error("登录退出校验异常", e);
        return WebServiceResponse.fail(ResultCodeEnum.PERMISSION.getCode(), e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = IllegalArgumentException.class)
    public WebServiceResponse handler(IllegalArgumentException e) {
        log.error("Assert 异常",e);
        return WebServiceResponse.fail(ResultCodeEnum.PERMISSION.getCode(), e.getMessage());
    }

    /** Validated 校验错误异常处理*/
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public WebServiceResponse handler(MethodArgumentNotValidException e) {
        log.error("实体类校验异常",e);
        return WebServiceResponse.fail(ResultCodeEnum.PERMISSION.getCode(), e.getMessage());
    }

    /** 运行时异常 */
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = RuntimeException.class)
    public WebServiceResponse handler(RuntimeException e) {
        log.error("运行时异常",e);
        return WebServiceResponse.fail(ResultCodeEnum.PERMISSION.getCode(), e.getMessage());
    }
}
